home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 May / Macworld (1998-05).dmg / Serious Demos / Lasso 2.5 Test Drive / Lasso 2.5 CGI / Java / DBInfo / DBInfo.java < prev    next >
Text File  |  1997-12-12  |  6KB  |  226 lines

  1. /*
  2.     An applet which uses the LassoProxy API to get information about the currently open databases
  3.     and show it in list boxes.
  4.     Double-clicking on an item in a list shows information about that item. For instance, double-clicking on 
  5.     a database name shows all its layouts. Double-clicking on a layout name show all its fields.
  6.     Double-clicking on a field name shows its value lists (if any).
  7.     
  8.     - Kyle Jessup    kylej@blueworld.com
  9. */
  10.  
  11. import java.awt.*;
  12. import java.applet.Applet;
  13. import java.io.IOException;
  14. import java.net.URL;
  15. import java.net.MalformedURLException;
  16.  
  17. public class DBInfo extends Applet
  18. {
  19.     protected    List            fDatabases = null;
  20.     protected    List            fLayouts;
  21.     protected    List            fFields;
  22.     protected    List            fValues;
  23.     protected    LassoProxy        fProxy;
  24.     
  25.     // to run this stand-alone, set this to true, then change to URL in the getDocumentbase method
  26.     // to the url of the server
  27.     protected    final static boolean    gRunLocal = false;
  28.     
  29.     public void init()
  30.     {
  31.         if (fDatabases == null)
  32.         {
  33.             setBackground(Color.white);
  34.             String    l = getParameter("lasso_name");
  35.             if (l == null || l.length() == 0)
  36.             {
  37.                 System.out.println("Could not find \"lasso_name\" param.");
  38.                 return;
  39.             }
  40.             
  41.             setLayout(new GridBagLayout());
  42.             
  43.             fDatabases = new List(15, false);
  44.             fLayouts = new List(15, false);
  45.             fFields = new List(15, false);
  46.             fValues = new List(15, false);
  47.             
  48.             GBConstrainer.constrain(this, new Label("Databases"), 0, 0);
  49.             GBConstrainer.constrain(this, new Label("Layouts"), 0, 1);
  50.             GBConstrainer.constrain(this, new Label("Fields"), 0, 2);
  51.             GBConstrainer.constrain(this, new Label("Values"), 0, 3);
  52.             
  53.             GBConstrainer.constrain(this, fDatabases, 1, 0);
  54.             GBConstrainer.constrain(this, fLayouts, 1, 1);
  55.             GBConstrainer.constrain(this, fFields, 1, 2);
  56.             GBConstrainer.constrain(this, fValues, 1, 3);
  57.             
  58.             // create the proxy object we will use repeatedly
  59.             fProxy = new LassoProxy(getDocumentBase(), l);
  60.         }
  61.         else
  62.         {
  63.             fDatabases.clear();
  64.             fLayouts.clear();
  65.             fFields.clear();
  66.             fValues.clear();
  67.         }
  68.         
  69.         try
  70.         {    
  71.             // retrieve all the database names so we can show them
  72.             String[]    names = fProxy.databaseNames();
  73.             
  74.             // add each name to the list
  75.             for (int i = 0; i < names.length; ++i)
  76.                 fDatabases.addItem(names[i]);
  77.         }
  78.         catch(IOException e)
  79.         {    System.out.println(e.toString());    }
  80.         
  81.         repaint();
  82.     }
  83.     
  84.     public URL getDocumentBase()
  85.     {
  86.         try
  87.         {
  88.             if (gRunLocal)
  89.                 return new URL("http://209.19.18.245/");
  90.             return super.getDocumentBase();
  91.         }
  92.         catch(MalformedURLException e){}
  93.         return null;
  94.     }
  95.     
  96.     public boolean action(Event evt, Object what)
  97.     {
  98.         if (evt.target == fDatabases)
  99.             updateLayouts();
  100.         else if (evt.target == fLayouts)
  101.             updateFields();
  102.         else if (evt.target == fFields)
  103.             if (!showImage()) updateValues();
  104.         
  105.         return true;        
  106.     }
  107.     
  108.     // an image field was double-clicked
  109.     // retrieve a record at random and try to display the image
  110.     protected boolean showImage()
  111.     {
  112.         String    fieldName = fFields.getSelectedItem();
  113.         int        index = fieldName.indexOf(" - Image!");
  114.         if (index == -1) return false;
  115.         
  116.         
  117.         LassoRequest    req = new LassoRequest(getParameter("lasso_name"));
  118.         req.setDatabaseName(fDatabases.getSelectedItem());
  119.         req.setLayoutName(fLayouts.getSelectedItem());
  120.         req.setAction(LassoRequest.RANDOM);
  121.         
  122.         LassoProxy    proxy = new LassoProxy(getDocumentBase(), getParameter("lasso_name"));
  123.         try
  124.         {
  125.             LassoResponse    response = proxy.processRequest(req);    
  126.             req.setRecordID(response.recordID(0));
  127.             ImageWindow m = new ImageWindow(proxy.getGIF(req, fieldName.substring(0, index), 8, false));
  128.             m.resize(m.getImage().getWidth(m), m.getImage().getHeight(m));
  129.         }
  130.         catch(IOException e)
  131.         {    System.out.println(e.toString()); return false;    }
  132.         return true;
  133.     }
  134.     
  135.     // add the layout names from the selected database to the list
  136.     protected void updateLayouts()
  137.     {
  138.         showStatus("Updating layouts...");
  139.         String    dbName = fDatabases.getSelectedItem();
  140.         if (dbName == null || dbName.length() == 0) return;
  141.         try
  142.         {
  143.             
  144.             String[]    lNames = fProxy.layoutNames(dbName);
  145.             fLayouts.clear();
  146.             fFields.clear();
  147.             fValues.clear();
  148.             for (int i = 0; i < lNames.length; ++i)
  149.                 fLayouts.addItem(lNames[i]);
  150.         }
  151.         catch(IOException e)
  152.         {    System.out.println(e.toString());    }
  153.         showStatus("");
  154.     }
  155.     
  156.     // get all the field names from the selected database and layout and add them to the list.
  157.     protected void updateFields()
  158.     {
  159.         showStatus("Updating fields...");
  160.         try
  161.         {        
  162.             String    layoutName = fLayouts.getSelectedItem();
  163.             if (layoutName == null || layoutName.length() == 0) return;
  164.             
  165.             LassoRequest    request = new LassoRequest(fProxy.getLasso());
  166.             
  167.             request.setAction(LassoRequest.FIELD_INFO);
  168.             request.setDatabaseName(fDatabases.getSelectedItem());
  169.             request.setLayoutName(layoutName);
  170.             
  171.             LassoResponse    response = fProxy.processRequest(request);
  172.             fFields.clear();
  173.             fValues.clear();
  174.             
  175.             for (int i = 0; i < response.fieldNames().length; ++i)
  176.             {
  177.                 if (response.fieldType(response.fieldIndex(response.fieldNames()[i])) == LassoResponse.IMAGE)
  178.                 {
  179.                     fFields.addItem(response.fieldNames()[i] + " - Image!");
  180.                 }
  181.                 else
  182.                     fFields.addItem(response.fieldNames()[i]);
  183.             }
  184.         }
  185.         catch(IOException e)
  186.         {    System.out.println(e.toString());    }
  187.         catch(FieldNotFoundException e)
  188.         {    System.out.println(e.toString());    }
  189.         finally
  190.         {    showStatus("");                    }
  191.     }    
  192.     
  193.     // update the valuelists of the double-clicked field
  194.     protected void updateValues()
  195.     {
  196.         showStatus("Updating values...");
  197.         String    fieldName = fFields.getSelectedItem();
  198.         if (fieldName == null || fieldName.length() == 0) return;
  199.         
  200.         LassoRequest    request = new LassoRequest(fProxy.getLasso());
  201.         
  202.         request.setAction(LassoRequest.FIELD_INFO);
  203.         request.setDatabaseName(fDatabases.getSelectedItem());
  204.         request.setLayoutName(fLayouts.getSelectedItem());
  205.         try
  206.         {
  207.             LassoResponse    response = fProxy.processRequest(request);
  208.             fValues.clear();
  209.             int    index = response.fieldIndex(fieldName);
  210.             for (int i = 0; i < response.fieldValueList(index).length; ++i)
  211.                 fValues.addItem(response.fieldValueList(index)[i]);
  212.         }
  213.         catch(IOException e)
  214.         {    System.out.println(e.toString());    }
  215.         catch(FieldNotFoundException e)
  216.         {    System.out.println(e.toString());    }
  217.         finally
  218.         {    showStatus("");                    }
  219.     }        
  220.     
  221. }
  222.  
  223.  
  224.  
  225.  
  226.